import navToNext from '../../wxjs/navToNext.js'
const app = getApp();

let methods = {
  clickPraise, //点赞
  clickColl, //收藏
  openLocation,
  changeArticleLength,
};

var component = {
  properties: {
    item: {
      type: null,
      observer: function (newVal, oldVal) {
        const that = this;
        const item = newVal;
        if (item.releaseContent.length>60) {
          item.isAll = true;
          item.article = item.releaseContent.slice(0, 60) + "......";
        }else{
          item.article = item.releaseContent;
        }
        that.setData({
          item
        })
      }
    }
  },
  data: {
    xhx_images: app.xhx_images,
    xhx_route: app.xhx_route,
  },
  methods: Object.assign(navToNext, methods)
};

Component(component);


function clickPraise() {
  const that = this;
  const item = that.data.item || {};

  var myInfo = app.isNavigateToLogin();
  if (!myInfo) return;

  myInfo = app.isNavigateToBind();
  if (!myInfo) return;

  var reqData = {
    trendsId: item.uuid,
  };
  var serAddr = app.serAddrs.comfirmLike;

  if (item.likesStatus == 1) {
    serAddr = app.serAddrs.cancelLike;
  }

  app.middleware.requestAjax({
    serAddr: serAddr,
    data: reqData,
    success: function (res) {
      if (item.likesStatus == 1) {
        item.likesStatus = 0;
        item.likesCount--;
      } else {
        item.likesStatus = 1;
        item.likesCount++;
      }
      that.setData({
        item
      })
    },
    fail: function (res) {
      app.middleware.showToast(res && res.msg || "操作失败");
    }
  })
}

function clickColl() {
  const that = this;
  const item = that.data.item || {};

  var myInfo = app.isNavigateToLogin();
  if (!myInfo) return;

  myInfo = app.isNavigateToBind();
  if (!myInfo) return;

  var reqData = {
    trendsId: item.uuid,
  };
  var serAddr = app.serAddrs.comfirmColl;

  if (item.collectionStatus == 1) {
    serAddr = app.serAddrs.cancelColl;
  }

  app.middleware.requestAjax({
    serAddr: serAddr,
    data: reqData,
    success: function (res) {
      if (item.collectionStatus) {
        item.collectionStatus = 0;
        item.collectionCount--;
      } else {
        item.collectionStatus = 1;
        item.collectionCount++;
      }
      that.setData({
        item
      })
    },
    fail: function (res) {
      app.middleware.showToast(res && res.msg || "操作失败");
    }
  })
}

function openLocation(event) {
  const that = this;
  const item = that.data.item || {};
  const latitude = Number(item.latitude);
  const longitude = Number(item.longitude);
  app.middleware.openLocation({
    latitude,
    longitude,
  });
}

function changeArticleLength(){
  const that = this;
  const item = that.data.item || {};
  
  if (item.isAll){
    item.article = item.releaseContent;
  }else{
    item.article = item.releaseContent.slice(0, 60) + "......";
  }
  item.isAll = !item.isAll;
  that.setData({
    item
  })
}